model.os = {
		fetchUsers : function(user_ids, callback, options) {
			
			if (utils.empty(options)) options = {};
			
			var users = new Object();
						
			user_ids = utils.toList(user_ids);
			
		    var req = opensocial.newDataRequest();
		    var params = {};
		    var user;
		    var missing = new Array();
		    
		    $.each(user_ids, function(i, user_id) {
		    	
		    	user = model.storage.users.get(user_id);

		    	if (user == null) {
		    		req.add(req.newFetchPersonRequest(user_id, params), user_id);
		    		missing.push(user_id);
		    	} else {
		    		users[user_id] = user;
		    	}
		    });
		    
		    var onloaded = function(data) {

		    	$.each(missing, function(i, user_id) {

		    		person = data.get(user_id).getData();

		    		user = model.os.getUserInfo(person);
					if (user != null) users[user.id] = user;
		        });
		    	
		    	if ($.isFunction(callback)) {
		    		callback(users);
		    	}
		    }
		   
		    var noblock = !utils.empty(options.noblock);
		    
		    this.sendRequest(req, onloaded, noblock);
		},
		
		fetchFriends : function(callback) {
			
			if (model.storage.users.getFriends() != null) return model.storage.users.getFriends();

			var req = opensocial.newDataRequest();
			
			var users = new Object();
			
			var viewerFriends = opensocial.newIdSpec({ "userId" : "VIEWER", "groupId" : "FRIENDS" });
			var opt_params = {};
			opt_params[opensocial.DataRequest.PeopleRequestFields.MAX] = 100;
			req.add(req.newFetchPeopleRequest(viewerFriends, opt_params), 'friends');
	    	    
		    var onloaded = function(data) {

		    	var persons = data.get('friends').getData();
		    	
		    	persons.each(function(person) {

		    		user = model.os.getUserInfo(person);
					if (user != null) users[user.id] = user;
		        });
		    	
		    	if ($.isFunction(callback)) {
		    		model.storage.users.setFriends(users);
		    		callback(users);
		    	}
		    }
			
	    	this.sendRequest(req, onloaded);

		},
		
		getUserInfo : function(person) {
			try {
				user = {"photo" : person.getField(opensocial.Person.Field.THUMBNAIL_URL), 
						"url"  : person.getField(opensocial.Person.Field.PROFILE_URL),
						"gender"  : person.getField(opensocial.Person.Field.GENDER),
						"name"  : person.getField(opensocial.Person.Field.NAME).getField("givenName") + " " + person.getField(opensocial.Person.Field.NAME).getField("familyName"),
						"id"    : person.getId()};
				
				if (!utils.empty(user.photo)) user.photo = user.photo.replace(/-s./, "-m.");
				
				if (person.isViewer()) viewer = user;
				if (person.isOwner()) owner = user;

			} catch (e) {
				//console.log(person)
				return null;
			}
			
			model.storage.users.add(user);
			
			return user;
		},
		
		sendRequest : function(req, callback, noblock) {
			
			if (!noblock) view.common.lockScreen();
			
		    req.send(function(data) {
		    	
		    	if (!noblock) view.common.unlockScreen();
		    	
		    	callback(data);
		    });
		},
		
		sendMessage : function (user_id, title, body) {

			  var params = [];
			  params[opensocial.Message.Field.TITLE] = title;
			  params[opensocial.Message.Field.TYPE] = opensocial.Message.Type.NOTIFICATION;
			 
			  var message = opensocial.newMessage(body, params);

			  opensocial.requestSendMessage(user_id, message);
		}
};